/* 
 * File:   fsp.c
 * Author: Kevin Ouwens
 *
 * Version: 0.5
 *
 * Created on December 15, 2010, 2:32 PM
 * Last edit on January 24, 2010, 9:07 PM
 *
 * Full name: file server process
 *
 * Info:
 * This process is used in combination of the CHP (Client Handler Process).
 * The main function of this process is to accept new clients after they connected to the CHP
 * The FSP gets info from the CHP in a memmap file: NEWCLIENT 5 for example.
 * The user info will then be stored in a unique struct per user containing:
 * - Unique semaphores
 * - A unique memory map file for the client
 * - Clientnumber (which is used for identification)
 * - Filename of the file someone wants to up or dowload
 * - Current directory
 * - A personal buffer
 * - File descriptors
 * These are found in the hfile.h include.
 *
 */

//includes
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h> //filecontrol (open, write, etc.)
#include <semaphore.h> //semaforen
#include <sys/mman.h> //memory map files dingen
#include <string.h>
#include <pthread.h>
#include <unistd.h> //mutexes
#include "hfile.h"
//end of includes

//global variables.
int commChanfd = 0; //file descriptor to use with the communication channel between FSP and CHP
sem_t *semComChanRead;
sem_t *semComChanPost;
//end of global variables

//MAIN PROGRAM
int main(void)
{
    //variables
    char *comMap; //communication mmap
    pthread_mutex_t mutex; //mutex
    //end of variables
    //open the comChan semaphores
    openSem();

    //infinite loop to obtain data from the CHP
    while(1)
    {
        //wait for the commchanel semaphore
        if((sem_wait(semComChanPost))<0)
        {
            perror("sem_wait");
        }
        //end of wait

        //open the communication channel for IPC, CHP should create it because he is the first to talk to the FSP
        commChanfd = open(FILEPATH_COMMCHAN, O_RDWR, ACC_RIGHT);
        if(commChanfd < 0)
        {
            perror("Could not open the communication channel!");
        }
        printf("commChanfd: %d\n", commChanfd);
        //end of communication channel opening

        //map the comm channel
        comMap = mmap(0, CHUNKSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, commChanfd, 0);
        if (comMap == MAP_FAILED) {
            close(commChanfd);
            perror("Error mmapping the file");
            exit(EXIT_FAILURE);
        }
        //!!DEBUGGING!!
        printf("comMap inhoud: %s\n", comMap);
        //end of comm channel map

        //read the comMap
        //split the comMap up in pieces
        printf("going to split now\n");
        commandAna(comMap);
        close(commChanfd);
        if((sem_post(semComChanRead))<0)
        {
            perror("error posting read");
        }
        //end of split
        //end of read
    }
    //if all is ok, this will never be reached, and it should never be reached
    return 0;
}
//END OF MAIN PROGRAM

/*
 * openSem: open semaphores
 * Info:
 * This function is used to create and open all semaphores that will be used in the program
 */
int openSem()
{
    if((semComChanRead = sem_open("semComChanRead", 0))== SEM_FAILED)
    {
        perror("semopen semComChanRead: ");
        exit(EXIT_FAILURE);
    }
    if((semComChanPost = sem_open("semComChanPost", 0))== SEM_FAILED)
    {
        perror("semopen semComChanPost: ");
        exit(EXIT_FAILURE);
    }
    //end of initialization of mutexes and semaphores
    return 0;
}
//END OF OPENSEM